package com.darkglasses.bm.controller;

import com.darkglasses.bm.utils.FileWriteUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping
public class IndexController {

    private FileWriteUtil fileWriteUtil;
    @PostConstruct
    public void init() {
        System.out.println(IndexController.class.getClassLoader().getResource("application.properties"));
        String temp = IndexController.class.getClassLoader().getResource("static/data/bm.txt").getFile();
        System.out.println("文件:"+temp);
        File file = new File(temp);
        System.out.println(file.exists());
        System.out.println(file.getAbsolutePath());
        fileWriteUtil = new FileWriteUtil(file);
    }

    // @GetMapping({"/", "index", "index.html"})
    public void defaultRequest(HttpServletResponse response) throws IOException {
        response.sendRedirect("index/index.html");
    }

    // @GetMapping("index/index.html")
    @GetMapping({"/", "index", "index.html"})
    public void getIndex(@CookieValue(value = "you", required = false) String exists,
                         HttpServletResponse response) throws IOException {
        //System.out.println("get msg");
        if ("dog?".equals(exists)) {
            response.sendRedirect("index/success.html");
            //return "index/success.html";
            return;
        }
        // 创建一个 cookie对象
        Cookie cookie = new Cookie("you", "goodPeople");
        cookie.setHttpOnly(true);  //不能被js访问的Cookie
        //将cookie对象加入response响应
        response.addCookie(cookie);
        response.sendRedirect("index/index.html");
       // return "index/index.html";
    }

    @GetMapping("signUp")
    @ResponseBody
    public void signUp(@CookieValue(value = "you", required = false) String exists,
                         @RequestParam("n")String name,
                         @RequestParam("c")String className,
                         @RequestParam("p")String phoneNumber,
                         @RequestParam(value = "cT", defaultValue = "")String cType,
                         @RequestParam(value = "b", defaultValue = "")String bzrName,
                         @RequestParam(value = "z", defaultValue = "")String zdlsName,
                         HttpServletResponse response) throws IOException {
        String data = name + " " + className + " " + phoneNumber + " " + cType + " " + bzrName + " " + zdlsName + "\n";
        // System.out.println("#:"+data+" ---> "+exists);
        if (null == exists) {
            response.sendRedirect("index/reScan.html");
            return ;
        }else if ("dog?".equals(exists)) {
            response.sendRedirect("index/success.html");
            return ;
        }
        int temp = name.trim().length();
        if (temp == 0 || temp > 10) {
            response.sendRedirect("index/defeat.html");
            return ;
        }
        temp = className.trim().length();
        if (temp == 0 || temp > 10) {
            response.sendRedirect("index/defeat.html");
            return ;
        }
        temp = phoneNumber.trim().length();
        if (temp == 0 || temp > 13) {
            response.sendRedirect("index/defeat.html");
            return ;
        }
        temp = bzrName.trim().length();
        if (temp == 0 || temp > 10) {
            bzrName = "";
        }
        temp = zdlsName.trim().length();
        if (temp == 0 || temp > 10) {
            zdlsName = "";
        }

        // System.out.println(data);
        // 创建一个 cookie对象
        Cookie cookie = new Cookie("you", "dog?");
        cookie.setHttpOnly(true);  //不能被js访问的Cookie
        //将cookie对象加入response响应
        response.addCookie(cookie);
        fileWriteUtil.write(data);
        response.sendRedirect("index/success.html");
    }
}
